{% extends 'plugins/plugin_base.html' %}{% load i18n static thumbnail %}
{% block extrajs %}
{{ block.super }}
<script type="text/javascript" src="{% static "js/jquery.cookie.js" %}"></script>
<script type="text/javascript" src="{% static "jquery-ratings/jquery.ratings.js" %}"></script>
<script type="text/javascript">

    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    jQuery(document).ajaxStart(function () {
            jQuery('#loading').show();
        }).ajaxStop(function () {
            jQuery('#loading').hide();
    });

    // Rating
    jQuery(function(){
        var csrftoken = jQuery.cookie('csrftoken');
        jQuery.ajaxSetup({
            crossDomain: false, // obviates need for sameOrigin test
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });
        jQuery('#rating').ratings(5, {% firstof rating '0' %}).bind('ratingchanged', function(event, data) {
            jQuery.post('{% url "plugin_rate" object.pk '0' %}'.replace('/0', '/' + data.rating),  function(data){
                if(data.search('changed') == -1){
                    has_voted = true;
                    jQuery('#votes').html(parseInt(jQuery('#votes').html())+1);
                }
                jQuery('#vote-message').html(data).fadeIn('slow', function(){jQuery(this).fadeOut('slow');});
            });
        });
    });
</script>
{% endblock %}
{% block extracss %}
<link type="text/css" href="{% static "jquery-ratings/jquery.ratings.css" %}" rel="stylesheet" />
{{ block.super }}
{% endblock %}
{% block content %}
    <div class="row">
        {% if object.stable or object.experimental %}
        <div class="span2">
            <h2>
            <a class="btn btn-block btn-primary" href="{% if object.stable %}{{ object.stable.get_download_url }}{% else %}{{ object.experimental.get_download_url }}{% endif %}"><i class="icon-download icon-white icon-2x" style=" vertical-align: middle;"></i>&nbsp;{% trans "Download latest" %}</a>
            </h2>
        </div>
        <div class="span10">
        {% else %}
        <div class="span12">
        {% endif %}
            <h2>{{ object.name }}
            {% if object.icon and object.icon.file %}
                {% thumbnail object.icon "128x128" upscale=False format="PNG" as im %}
                    <img class="pull-right plugin-icon" alt="{% trans "Plugin icon" %}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}" />
                {% endthumbnail %}
            {% endif %}
            </h2>
        </div>
    </div>
    <div>
        {% if not object.experimental and not object.stable %}
        <div class="alert alert-warning">
            <p>{% trans "This plugin has no public version yet." %}</p>
        </div>
        {% endif %}

        {% if not object.created_by.is_active %}
         <div class="alert alert-warning">
            <p>{% trans "The plugin maintainer has been blocked." %}</p>
        </div>
        {% endif %}

        {% if object.deprecated %}
        <div class="alert alert-error">
            <p class="error">{% trans "This plugin is deprecated!" %}</p>
        </div>
        {% endif %}

        <div id="rating"> (<span id="votes">{% firstof votes '0' %}</span>) {% trans "votes" %}&nbsp;<span id="loading" style="display:none">Loading...</span><span id="vote-message" style="display:none"></span></div>
        <blockquote>
            <p class="plugin-details-description">{{ object.description|safe|linebreaksbr }}</p>
        </blockquote>
        {% comment%}
        {% if object.about %}
        <div class="well"><p>{{ object.about|safe|linebreaksbr }}</p></div>
        {% endif %}
        {% endcomment %}

        <ul class="nav nav-tabs" id="plugintab">
            {% if object.about %}
            <li class="active"><a href="#plugin-about" data-toggle="tab">{% trans "About" %}</a></li>
            {% endif %}
            <li {% if not object.about %}class="active"{% endif %}><a href="#plugin-details" data-toggle="tab">{% trans "Details" %}</a></li>
            <li><a href="#plugin-versions" data-toggle="tab">{% trans "Versions" %}</a></li>
            {% if user.is_staff or user in object.editors %}
            <li><a href="#plugin-manage" data-toggle="tab">{% trans "Manage" %}</a></li>
            {% endif %}
        </ul>

        <div class="tab-content">
            {% if object.about %}
            <div class="tab-pane active" id="plugin-about">
                <p>{{ object.about|safe|linebreaksbr }}</p>
            </div>
            {% endif %}
            <div class="tab-pane{% if not object.about %} active{% endif %}" id="plugin-details">
                {% if object.server %}
                <div class="alert alert-info fade in">{% trans "This plugin provides an interface for QGIS Server." %}</div>
                {% endif %}
                <dl class="dl-horizontal">
                    {% if object.author %}
                        <dt>{% trans "Author"%}</dt>
                        <dd>
                            <a title="{% trans "Plugins by"%} {{ object.author }}" href="{% url "author_plugins" object.author|urlencode %}">{{ object.author }}</a>
                        </dd>
                    {% endif %}
                    {% if object.email and not user.is_anonymous %}
                        <dt>{% trans "Author's email"%}</dt>
                        <dd> <a href="mailto:{{ object.email }}">{{ object.email }}</a></dd>
                    {% endif %}
                    <dt>{% trans "Maintainer"%}</dt>
                    <dd>
                        <a href="{% url "user_details" object.created_by %}">{{ object.created_by }}</a>
                    </dd>
                    {% if object.owners.count %}
                        <dt>{% trans "Owners"%}</dt>
                        <dd>
                            {% for owner in object.owners.all %}
                            <a href="{% url "user_details" owner.username %}">{{ owner.username }}</a>{% if not forloop.last %},{% endif %}
                            {% endfor %}
                        </dd>
                    {% endif %}
                    {% if object.tags.count %}
                    <dt>
                        {% trans "Tags"%}
                    </dt>
                    <dd>
                        {% for tag in object.tags.all %}{% if tag.slug %}<a href="{% url "tags_plugins" tag.slug %}">{{tag}}</a>
                        {% if not forloop.last %},&nbsp;{% endif %}{% endif %}{% endfor %}
                    </dd>
                    {% endif %}
                    {% if object.homepage %}
                        <dt>{% trans "Plugin home page"%}</dt>
                        <dd> <a href="{{ object.homepage }}">{{ object.homepage }}</a></dd>
                    {% endif %}
                    {% if object.tracker %}
                        <dt>{% trans "Tracker" %}</dt>
                        <dd> <a href="{{ object.tracker }}">{% trans "Browse and report bugs" %}</a></dd>
                    {% endif %}
                    {% if object.repository %}
                        <dt>{% trans "Code repository" %}</dt>
                        <dd> <a href="{{ object.repository }}">{{ object.repository }}</a></dd>
                    {% endif %}
                    {% if object.stable %}
                        <dt>{% trans "Latest stable version"%}</dt>
                        <dd> <a title="{% trans "Click to download" %}"  href="{% url "version_detail" object.package_name object.stable.version %}">{{ object.stable.version }}</a></dd>
                    {% endif %}
                    {% if object.experimental %}
                        <dt>{% trans "Latest experimental version"%}:</dt>
                        <dd> <a title="{% trans "Click to download" %}" href="{% url "version_detail" object.package_name object.experimental.version %}">{{ object.experimental.version }}</a></dd>
                    {% endif %}
                </dl>
            </div>
            <div class="tab-pane" id="plugin-versions">
                {% if object.pluginversion_set.count %}
                {# show all versions if user is authorized #}
                <div id="version_list">
                    <table class="table table-striped plugins">
                        <thead>
                            <tr>
                                <th>{% trans "Version" %}</th>
                                {% if not user.is_anonymous %}<th>{% trans "Approved" %}</th>{% endif %}
                                <th>{% trans "Experimental" %}</th>
                                <th>{% trans "Minimum QGIS version" %}</th>
                                <th>{% trans "Downloads" %}</th>
                                <th>{% trans "Uploaded by" %}</th>
                                <th>{% trans "Date" %}</th>
                                {% if user.is_staff or user in object.approvers or user in object.editors %}<th>{% trans "Manage" %}</th>{% endif %}
                            </tr>
                        </thead>
                        <tbody>
                        {% for version in object.pluginversion_set.all %}
                            {% if version.approved or not user.is_anonymous %}
                            <tr class="{% cycle "even" "odd" %}">
                                <td><a title="{% trans "Version details" %}" href="{% url "version_detail" object.package_name version.version %}">{{ version.version }}</a></td>
                                {% if not user.is_anonymous %}<td>{{ version.approved|yesno }}</td>{% endif %}
                                <td>{{ version.experimental|yesno }}</td>
                                <td>{{ version.min_qg_version }}</td>
                                <td>{{ version.downloads }}</td>
                                <td><a href="{% url "user_details" version.created_by.username %}">{{ version.created_by }}</a></td>
                                <td>{{ version.created_on }}</td>
                                {% if user.is_staff or user in version.plugin.approvers or user in version.plugin.editors %}<td><form method="post" action="{% url "version_manage" object.package_name version.version %}">{% csrf_token %}
                                {% if user.is_staff or user in version.plugin.approvers %}
                                    {% if not version.approved %}<button class="btn btn-success btn-mini" type="submit" name="version_approve" id="version_approve"title="{% trans "Approve" %}"><i class="icon-thumbs-up icon-white"></i></button>{% else %}<button class="btn btn-warning btn-mini" type="submit" name="version_unapprove" id="version_unapprove" title="{% trans "Unapprove" %}"><i class="icon-thumbs-down icon-white"></i></button>{% endif %}
                                {% endif %}
                                {% if user.is_staff or user in version.plugin.editors %}
                                <a class="btn btn-primary btn-mini" href="{% url "version_update" object.package_name version.version %}" title="{% trans "Edit" %}"><i class="icon-pencil icon-white"></i></a>&nbsp;<a class="btn btn-danger btn-mini delete" href="{% url "version_delete" object.package_name version.version %}" title="{% trans "Delete" %}"><i class="icon-remove icon-white"></i></a>{% endif %}</form>
                                </td>
                                {% endif %}
                            </tr>
                            {% endif %}
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% endif %}
            </div>
            <!-- manager functions -->
            {% if user.is_staff or user in object.editors %}
            <div class="tab-pane" id="plugin-manage">
                <form method="post" action="{% url "plugin_manage" object.package_name %}">{% csrf_token %}
                <div>
                        <a class="btn btn-primary" href="{% url "plugin_update" object.package_name %}">{% trans "Edit" %}</a>
                        <a class="btn btn-primary" href="{% url "version_create" object.package_name %}">{% trans "Add version" %}</a>
                    {% if user.is_staff  %}
                        {% if object.featured %}<input class="btn btn-warning" type="submit" name="unset_featured" id="unset_featured" value="{% trans "Unset featured" %}" />
                        {% else %}<input class="btn btn-primary" type="submit" name="set_featured" id="set_featured" value="{% trans "Set featured" %}" />
                        {% endif %}
                    {% endif %}
                    {% if user.is_staff or user in object.editors %}
                        <a class="btn btn-danger" class="delete" href="{% url "plugin_delete" object.package_name %}">{% trans "Delete" %}</a>
                    {% endif %}
                </div>
                </form>
            </div>
            {% endif %}
            {# end admin #}
        </div><!-- tab content -->
    </div>
{% endblock %}
